package graduate.cslg.com.universityshopping.dormitory;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CheckOutActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText stuPhone;
    private EditText stuReason;
    private Spinner mSpinner;
    private Button submitButton;
    private String[] typeName = {"休学", "走读", "校外住宿", "退学"};
    private String getUserAccount;
    private String getTypeName;
    private LoginResponse mLoginResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_out);

        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315222");
        initViews();
        ArrayAdapter<String> type_adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item,typeName);
        mSpinner.setAdapter(type_adapter);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                getTypeName = typeName[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void initViews() {
        stuPhone = findViewById(R.id.stu_tel);
        stuReason = findViewById(R.id.stu_reason);
        mSpinner = findViewById(R.id.reason_type);
        submitButton = findViewById(R.id.submit_button);
        submitButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_button:
                if (stuPhone.getText().toString().length() != 0 && stuReason.getText().toString().length() != 0) {
                    postCheckOutInfo(stuPhone.getText().toString(), stuReason.getText().toString(), getTypeName);
                } else {
                    Toast.makeText(this, "请检查信息输入！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void postCheckOutInfo(String stuTel, String stuReason, String checkOutReason) {
        RetrofitDevelop.getTestService()
                .addCheckOutInfo(stuTel, stuReason, checkOutReason, getUserAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Toast.makeText(CheckOutActivity.this, "信息提交成功!", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(CheckOutActivity.this, "提交失败，请稍候再试！", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(CheckOutActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }
}
